﻿//**************************
//开发人员：Morning Sun QQ:576830428  微信:MorningSun0125
//业务范围：网站开发、小程序开发、web应用开发,桌面程序开发,工控类软件开发等
//开发日期：2016年1月10日
//版本号：Version 3.0 CopyRight 2011-2029
//**************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FileServer
{
    public partial class FormServerConfig : Form
    {
        #region 变量
        /// <summary>
        /// 拖动位置
        /// </summary>
        private Point MousePoint;
        /// <summary>
        /// 是否为鼠标左键
        /// </summary>
        private bool LeftFlag;

        /// <summary>
        /// 服务器信息
        /// </summary>
        DalTbServer dalserver = new DalTbServer();
        #endregion


        #region 构造函数
        public FormServerConfig()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            CenterToScreen();

            #region 工具栏
            WinIcon.Parent = WinTitlePic;
            labWinTitle.Parent = WinTitlePic;
            labWinMin.Parent = WinTitlePic;
            labWinClose.Parent = WinTitlePic;
            labWinMin.BackgroundImage = global::FileServer.Properties.Resources.winMin;
            labWinClose.BackgroundImage = global::FileServer.Properties.Resources.winClose;
            #endregion

            #region 工作区
            PBox.Parent = WorkPic;
            #endregion

            #region 加载信息
            DataSet ds = dalserver.GetTopList(1,"","");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                LabID.Text = dr["ID"].ToString().Trim();
                TbxIP.Text = dr["ServerIP"].ToString().Trim();
                TbxPort.Text = dr["ServerPort"].ToString().Trim();
                TbxDir.Text = dr["SyncDir"].ToString().Trim();
                TbxFilter.Text = dr["SyncFilter"].ToString().Trim();
                RtbExclude.Text = dr["ExcludeDir"].ToString().Trim();
                if (dr["ChildDir"].ToString().Trim() == "True")
                {
                    SyncChild.Checked = true;
                }
                else
                {
                    SyncChild.Checked = false;
                }
            }
            #endregion
        }
        #endregion


        #region   最小化窗体
        /// <summary>
        /// 最小化窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion


        #region 关闭窗体
        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion


        #region  最小化按钮效果
        /// <summary>
        /// 鼠标悬浮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinMin_MouseHover(object sender, EventArgs e)
        {
            labWinMin.BackgroundImage = global::FileServer.Properties.Resources.winMinPressed;
        }


        /// <summary>
        /// 鼠标离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinMin_MouseLeave(object sender, EventArgs e)
        {
            labWinMin.BackgroundImage = global::FileServer.Properties.Resources.winMin;
        }
        #endregion


        #region  关闭按钮效果
        /// <summary>
        /// 关闭按钮悬浮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinClose_MouseHover(object sender, EventArgs e)
        {
            labWinClose.BackgroundImage = global::FileServer.Properties.Resources.winClosePressed;
        }


        /// <summary>
        /// 关闭按钮鼠标经过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinClose_MouseLeave(object sender, EventArgs e)
        {
            labWinClose.BackgroundImage = global::FileServer.Properties.Resources.winClose;
        }
        #endregion


        #region 窗体拖动
        private void WinTitlePic_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MousePoint = new Point(-e.X, -e.Y); //得到变量的值
                LeftFlag = true;                  //点击左键按下时标注为true;
            }
        }

        private void WinTitlePic_MouseMove(object sender, MouseEventArgs e)
        {
            if (LeftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(MousePoint.X, MousePoint.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void WinTitlePic_MouseUp(object sender, MouseEventArgs e)
        {
            if (LeftFlag)
            {
                LeftFlag = false;//释放鼠标后标注为false;
            }
        }
        #endregion


        #region  按钮效果
        /// <summary>
        /// 鼠标离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = global::FileServer.Properties.Resources.btn;
        }


        /// <summary>
        /// 鼠标悬浮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = global::FileServer.Properties.Resources.btn_over;
        }
        #endregion




        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string CheckResult = CheckConfig();
            if (CheckResult == "")
            {
                TbServer server = new TbServer();
                server.ServerIP = TbxIP.Text.Trim();
                server.ServerPort =int.Parse(TbxPort.Text.Trim());
                server.SyncDir = TbxDir.Text.Trim().Replace("：",":");
                server.SyncFilter = TbxFilter.Text.Trim() == "" ? "*.*" : TbxFilter.Text.Trim();
                server.ChildDir = SyncChild.Checked.ToString();
                server.ExcludeDir = RtbExclude.Text.Trim().Replace("：", ":").Replace("；", ";");

                int result = 0;
                if (LabID.Text.Trim() != "0")
                {
                    server.ID = int.Parse(LabID.Text.Trim());
                    result = dalserver.UpDateByModel(server);
                }
                else
                {
                    result = dalserver.AddByModel(server);
                }
                if (result > 0)
                {
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show(CheckResult, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion


        #region 关闭
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion


        #region 检测配置
        /// <summary>
        /// 检测配置
        /// </summary>
        /// <returns></returns>
        private string CheckConfig()
        {
            string IP = TbxIP.Text.Trim();
            string Port = TbxPort.Text.Trim();
            string Result = "";
            if (FormValidate.IsEmpt(IP))
            {
                Result = "请填写服务器IP!";
            }
            else if (FormValidate.IsEmpt(Port))
            {
                Result = "请填写服务器端口!";
            }
            else if (!FormValidate.IsIP(IP))
            {
                Result = "服务器IP格式错误!";
            }
            else if (!FormValidate.IsNumber(Port))
            {
                Result = "服务器端口必须是数字!";
            }
            else if (FormValidate.IsEmpt(TbxDir.Text.Trim()))
            {
                Result = "同步目录不能为空!";
            }            
            else if (!Directory.Exists(TbxDir.Text.Trim()))
            {
                Result = "同步目录不存在!";
            }
            else if (RtbExclude.Text.Trim()!="")
            {
                string[] DirArr = RtbExclude.Text.Trim().Replace("：", ":").Replace("；", ";").Split(';');
                foreach (string dir in DirArr)
                {
                    if (dir!=""&&!dir.Contains(TbxDir.Text.Trim()))
                    {
                        Result = "排除目录配置错误!";
                        break;
                    }
                }                
            }
            return Result;
        }
        #endregion
    }
}
